package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.DynamicInfo;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.util.PalLog;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrater on 2015/12/21.
 */
public class LiveLiuYanAdapter extends BaseAdapter {
    private Context mContext;
    private List<DynamicInfo> mList = new ArrayList<DynamicInfo>();
    private HashMap<Integer,Boolean> hashMap = new HashMap<Integer, Boolean>();

    public LiveLiuYanAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<DynamicInfo> list) {
        if (mList != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mList != null) {
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mList != null) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ChildHolder childHolder = null;
        if (convertView == null || convertView.getTag() == null) {
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.liveliuyan_item, null);
            childHolder.nick = (TextView) convertView.findViewById(R.id.nickname);
            childHolder.neirong = (TextView) convertView.findViewById(R.id.neirong);
            childHolder.dengji = (TextView) convertView.findViewById(R.id.dengji);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }

        final DynamicInfo info = mList.get(position);
        if(info.getUserName()!=null){
            try {
                childHolder.nick.setText(URLDecoder.decode(info.getUserName(),"utf-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        childHolder.dengji.setText(info.getUserLevel());
        int from = info.getDynamicType();
            childHolder.neirong.setTextColor(mContext.getResources().getColor(R.color.comment_live_alpa));
            if(from==0){
                childHolder.neirong.setText("进入房间");
            }else if(from==1){
                childHolder.neirong.setText(info.getCommentContent());
            }else if (from ==2){
                if (mListener != null){
                    EatShowUserInfo userInfo = new EatShowUserInfo();
                    userInfo.setUserId(info.getUserId());
                    userInfo.setUserLogo(info.getUserLogo());
                    userInfo.setNickName(info.getUserName());
                    userInfo.setUserLevel(info.getUserLevel());
                    boolean isZxdh = false;
                    if (hashMap.containsKey(position)){
                        isZxdh = hashMap.get(position);
                    }
                    PalLog.e("liveliuyanAdapter",info.getComboCount()+"连送礼物数量");
                    mListener.adapterLiwu(userInfo,childHolder.neirong,info.getCommentContent(),info.getComboCount(),isZxdh);
                    hashMap.put(position,true);

                }
            }else if (from ==3){
                childHolder.neirong.setText("分享了直播");
            }else if (from == 4){

            }else if(from == 5){
                if (mListener != null){
                    mListener.OnLiveStop();
                }
            }else if (from == 6){
                childHolder.neirong.setText("关注了主播");
            }
        return convertView;
    }

    private LiwuListener mListener;
    public void setLiwuListener(LiwuListener listener){
        mListener = listener;
    }

    public interface LiwuListener{
        public void adapterLiwu(EatShowUserInfo info, TextView textView, String giftId, int count, boolean isZx);

        public void OnLiveStop();
    }

    class ChildHolder {
        TextView nick;
        TextView neirong;
        TextView dengji;
    }
}
